﻿package classes.craft
{
	import flash.display.Stage;
	import flash.events.KeyboardEvent;
	import classes.craft.weapons.DefaultWeapon;
	import classes.craft.weapons.ICraftWeapon;
	import flash.ui.Keyboard;
	import flash.events.Event;
	import classes.events.CraftMoveEvent;
	/**
	 * ...
	 * @author ...
	 */
	public class CaveCraft extends BaseCraft
	{
		//static variables
		public static var shipAngle:uint;
		public static var weapons:Array = new Array();
		
		private var down:Boolean = false;
		private var up:Boolean = false;
		private var left:Boolean = false;
		private var right:Boolean = false;
		private var shoot:Boolean = false;


		private var friction:Number = 0.05;

		// get variable from Aerodynamics set
		private var turnSpdArray:Array = [2, 5, 7, 25];
		private var turnSpd:Number = 2;
		//

		public var spdX:Number = 0;
		public var spdY:Number = 0;

		// get variables from engine. 
		private var accArray:Array = [0.2, 0.5, 1.0];
		private var maxSpdArray:Array = [10, 20, 30];
		
		
		private var acceleration:Number = 0.2;
		private var maxSpd:Number = 10;
		//
		private var currentSpd:Number;
		
		private var currentWeapon:ICraftWeapon;		
		
		
		//public static var shield:CraftShield;
		private var mainStage:Stage;

		private function init():void
		{
			//CaveCraft.addWeapons();
			this.setWeapon(new DefaultWeapon());
			this.x = 500;
			this.y = 300;
		}
		public function CaveCraft(mainStage:Stage):void
		{
			init();
			CaveCraft.setAngle(this.rotation);
			this.mainStage = mainStage;
			
			mainStage.addEventListener(KeyboardEvent.KEY_DOWN,handleKeyDown);
			mainStage.addEventListener(KeyboardEvent.KEY_UP,handleKeyUp);
			mainStage.addEventListener(Event.ENTER_FRAME,getSpd);
		}

		public function setWeapon(newWeapon:ICraftWeapon):void
		{
			this.currentWeapon = newWeapon;
		}

		public function getWeapon():ICraftWeapon
		{
			return this.currentWeapon;
		}

		public static function addWeapons(newWeapon:ICraftWeapon):void
		{
			CaveCraft.weapons.push(newWeapon);
		}

		/*public static function setShield(newShield:ICrafShield):void
		{
		CaveCraft.shield = newShield;
		}*/
	
		private function launchCannon():void 
		{
			this.currentWeapon.shoot(this.mainStage,this.x,this.y,this.rotation);
		}
		
		public function handleKeyDown(e:KeyboardEvent)
		{
			switch (e.keyCode)
			{
				case Keyboard.RIGHT :
					right = true;
					break;
				case Keyboard.LEFT :
					left = true;
					break;
				case Keyboard.UP :
					up = true;
					break;
				case Keyboard.DOWN :
					down = true;
					break;
				case Keyboard.SPACE :
					shoot = true;
					break;
			}
		}
		
		public function handleKeyUp(e:KeyboardEvent)
		{
			switch (e.keyCode)
			{
				
				case 49 :
					craftThrust.gotoAndStop(1);
					craftWing.gotoAndStop(1);
					craftBase.gotoAndStop(1);
					
					turnSpd = turnSpdArray[craftWing.currentFrame-1];
					maxSpd = maxSpdArray[craftThrust.currentFrame-1];
					acceleration = accArray[craftThrust.currentFrame-1];
					break;
				case 50 :
					craftThrust.gotoAndStop(2);
					craftWing.gotoAndStop(2);
					craftBase.gotoAndStop(2);
					
					turnSpd = turnSpdArray[craftWing.currentFrame-1];
					maxSpd = maxSpdArray[craftThrust.currentFrame-1];
					acceleration = accArray[craftThrust.currentFrame-1];
					break;
					
				case 51 :
					craftThrust.gotoAndStop(3);
					craftWing.gotoAndStop(4);
					craftBase.gotoAndStop(3);
					
					turnSpd = turnSpdArray[craftWing.currentFrame-1];
					maxSpd = maxSpdArray[craftThrust.currentFrame-1];
					acceleration = accArray[craftThrust.currentFrame-1];
					break;
					
				case Keyboard.RIGHT :
					right = false;
					break;
				case Keyboard.LEFT :
					left = false;
					break;
				case Keyboard.UP :
					up = false;
					break;
				case Keyboard.DOWN :
					down = false;
					break;
				case Keyboard.SPACE :
					shoot = false;
					break;
			}
		}
		public static function setAngle(rotation:uint):void
		{
			CaveCraft.shipAngle = rotation;
		}
		
		public function getSpd(e:Event):void
		{
			currentSpd = Math.sqrt(spdX*spdX+spdY*spdY);

			if (right == true)
			{
				// variable needs to be fetched from aerodynamics set
				this.rotation +=  turnSpd;
			}
			if (left == true)
			{
				// variable needs to be fetched from aerodynamics set
				this.rotation -=  turnSpd;
			}
			if (up == true && currentSpd < maxSpd)
			{
				spdX +=  acceleration * Math.cos(this.rotation * Math.PI / 180);
				spdY +=  acceleration * Math.sin(this.rotation * Math.PI / 180);
			}
			if (down == true)
			{

			}
			if (shoot == true)
			{
				launchCannon();
			}
			if (spdX > 0)
			{
				spdX -=  friction;
			}
			if (spdY > 0)
			{
				spdY -=  friction;
			}
			if (spdX < 0)
			{
				spdX +=  friction;
			}
			if (spdY < 0)
			{
				spdY +=  friction;
			}
			dispatchEvent(new CraftMoveEvent(CraftMoveEvent.MOVE,spdX,spdY));
		}
		
	}

}